import 'package:aio/aio.dart';
import 'package:app/src/http/bean/clockin/attendances.dart';
import 'package:app/src/manager/user_manager.dart';
import 'package:intl/intl.dart';

///打卡请求数据
class ClokinSubDataType extends BaseBean {
  static String CheckTimeIn = "CheckTimeIn"; //打卡签到
  static String CheckTimeOut = "CheckTimeOut"; //打卡签退
  static String GetBaseData = "GetBaseData"; //获取班组经纬度信息（当前用户打卡的最新一条记录）。
  static String GetGroupChekTimeList = "GetGroupChekTimeList"; //获取签到记录
  static String GetSignOut = "GetSignOut"; //判断是否能签退；
  static String GetWageSettlement = "GetWageSettlement"; //工资结算
  static String GetGroupBcDateByGroupId =
      "GetGroupBcDateByGroupId"; //感觉班组获取对应的班次集合。

  static ClokinSubDataType getClokinSubDataType(String Type) {
    ClokinSubDataType clokinSubDataType = ClokinSubDataType();
    clokinSubDataType.Type = Type;
    clokinSubDataType.FactoryId = UserManager.getUserInfo()?.FactoryId;
    clokinSubDataType.PersonId = UserManager.getUserInfo()?.PersonId;
    return clokinSubDataType;
  }

  var Type = "GetBaseData"; //GetGroupInfo 获取班组班次; GetPointRange 获取经纬度
  String? FactoryId = ""; //工厂ID
  String? PersonId = ""; //用户id
  String? GroupId = ""; //班组id;
  String? Date = ""; //时间
  String? SignType = ""; //打卡类型，正常打卡；外勤打卡
  String? PicPath = ""; //图片路径
  String? AttendanceId = ""; //班次

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    Type = json["Type"]?.toString() ?? "";
    FactoryId = json["FactoryId"];
    PersonId = json["PersonId"];
    GroupId = json["GroupId"];
    Date = json["Date"];
    SignType = json["SignType"];
    PicPath = json["PicPath"];
    AttendanceId = json["AttendanceId"];
  }

  @override
  instance() {
    // TODO: implement instance
    return ClokinSubDataType();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["Type"] = Type;
    map["FactoryId"] = FactoryId;
    map["PersonId"] = PersonId;
    map["GroupId"] = GroupId;
    map["Date"] = Date;
    map["SignType"] = SignType;
    map["PicPath"] = PicPath;
    map["AttendanceId"] = AttendanceId;
    return map;
  }
}

///打卡提交数据
class ClokinSubmit extends BaseBean {
  String? type = "";
  String? factoryId = ""; //工厂ID
  String? personId = ""; //用户id
  String? groupId = ""; //班组id;
  String? workStartTime = ""; //班次开始时间
  String? workEndTime = ""; //班次结束时间
  String? OnWorkAdress = ""; //打卡地址
  bool isOnWorkField = false; //是否外勤打卡
  String? OnWorkImageUrl = ""; //打卡图片,默认值为空字符串。null会报错。

  ClokinSubmit() {
    factoryId = UserManager.getUserInfo()?.FactoryId;
    personId = UserManager.getUserInfo()?.PersonId;
  }

  ///判断是否为下班打卡。
  bool isOffWork() {
    if (type == ClokinSubDataType.CheckTimeOut) {
      return true;
    }
    return false;
  }

  MyGroupBcInfo? _GroupBcInfo = null;
  MyAttendances? _attendances = null;

  ///GroupBcInfo 选中班组
  ///attendances 选中班次
  ///Adress 打卡地址
  ///imageUrl fixme 打卡图片地址；最好不要为null；空值为""
  static ClokinSubmit getClokinSubmit(
      MyGroupBcInfo? GroupBcInfo, MyAttendances? attendances,
      {String? Adress,
      String? imageUrl,
      bool isOnWorkField = false,
      String? type = ""}) {
    ClokinSubmit data = ClokinSubmit();
    data._GroupBcInfo = GroupBcInfo;
    data._attendances = attendances;
    data.factoryId = UserManager.getUserInfo()?.FactoryId;
    data.personId = UserManager.getUserInfo()?.PersonId;
    data.groupId = GroupBcInfo?.GroupId;
    data.workStartTime = attendances?.OnWorkTime;
    data.workEndTime = attendances?.OffWorkTime;
    data.OnWorkAdress = Adress;
    data?.OnWorkImageUrl = imageUrl;
    if (imageUrl == null) {
      data?.OnWorkImageUrl = "";
    }
    data?.isOnWorkField = isOnWorkField;
    if (StringUtils.isNotEmpty(type)) {
      data?.type = type;
    }
    return data;
  }

  ///复制当前实体类
  ClokinSubmit copy({String? type = null}) {
    return getClokinSubmit(_GroupBcInfo, _attendances,
        Adress: OnWorkAdress,
        imageUrl: OnWorkImageUrl,
        isOnWorkField: isOnWorkField,
        type: type);
  }

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    type = json["type"]?.toString();
    factoryId = json["factoryId"]?.toString();
    personId = json["personId"]?.toString();
    groupId = json["groupId"]?.toString();
    workStartTime = json["workStartTime"]?.toString();
    workEndTime = json["workEndTime"]?.toString();
    OnWorkAdress = json["OnWorkAdress"]?.toString();
    isOnWorkField = json["isOnWorkField"] ?? false;
    OnWorkImageUrl = json["OnWorkImageUrl"]?.toString();
  }

  @override
  instance() {
    // TODO: implement instance
    return ClokinSubmit();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["type"] = type;
    map["factoryId"] = factoryId;
    map["personId"] = personId;
    map["groupId"] = groupId;
    map["workStartTime"] = workStartTime;
    map["workEndTime"] = workEndTime;
    map["OnWorkAdress"] = OnWorkAdress;
    map["isOnWorkField"] = isOnWorkField;
    map["OnWorkImageUrl"] = OnWorkImageUrl;
    return map;
  }
}

///打卡记录获取数据
class ClokinHistoricalRecord extends BaseBean {
  static int days = 366; //fixme 默认查询天数。
  String? type = ClokinSubDataType.GetGroupChekTimeList;
  String? factoryId = ""; //工厂ID
  String? OnWorkStartTime = ""; //签到开始时间
  String? OnWorkEndTime = ""; //签到结束时间;

  static ClokinHistoricalRecord getClokinSubmit(
      String? OnWorkStartTime, String? OnWorkEndTime,
      {String? type = ""}) {
    ClokinHistoricalRecord data = ClokinHistoricalRecord();
    data.factoryId = UserManager.getUserInfo()?.FactoryId;
    if (StringUtils.isEmpty(OnWorkStartTime) ||
        StringUtils.isEmpty(OnWorkEndTime)) {
      ///fixme 默认查询一年的签到记录。
      DateTime now = DateTime.now();
      //fixme 结束时间
      var lastDay =
          now.add(Duration(days: 2)); //fixme 结束时间比当前多两天；防止今天的时间，打明天的班次。
      var format = DateFormat("yyyy-MM-dd HH:mm:ss").format(lastDay);
      OnWorkEndTime = format;
      //fixme 开始时间
      DateTime time2 = now.add(Duration(days: -days));
      var format2 = DateFormat("yyyy-MM-dd HH:mm:ss").format(time2);
      OnWorkStartTime = format2;
    }
    data.OnWorkStartTime = OnWorkStartTime;
    data.OnWorkEndTime = OnWorkEndTime;
    if (StringUtils.isEmpty(type)) {
      data?.type = ClokinSubDataType.GetGroupChekTimeList;
    } else {
      data?.type = type;
    }
    return data;
  }

  @override
  fromJson(Map<String, dynamic> json) {
    // TODO: implement fromJson
    type = json["type"]?.toString();
    factoryId = json["factoryId"]?.toString();
    OnWorkStartTime = json["OnWorkStartTime"]?.toString();
    OnWorkEndTime = json["OnWorkEndTime"]?.toString();
  }

  @override
  instance() {
    // TODO: implement instance
    return ClokinHistoricalRecord();
  }

  @override
  Map<String, dynamic> toJson() {
    // TODO: implement toJson
    var map = <String, dynamic>{};
    map["type"] = type;
    map["factoryId"] = factoryId;
    map["OnWorkStartTime"] = OnWorkStartTime;
    map["OnWorkEndTime"] = OnWorkEndTime;
    return map;
  }
}
